<?php
/**
 * Verwalten von statischen Routen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 * @TODO es koennte noch ein caching-problem bestehen
 * */


Class Yapary_Route_DynamicRoute
{
    /** @var $oSql Yapary_Database_Sql */
    private $oSql           = NULL;
    /** @var $arData Array */
    private $arrData        = array();
    /** @var  $dbTableName string */
    private $dbTableName    = 'yapary_dynamic_route';
    /** @var int $elementId			: Id des Prospektes */
    private $elementId      = NULL;


    /**
     * @param Object $_oSql	: Datenbankklasse fuer Datenbankzugriff
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql, $_id)
    {
        $this->oSql        = $_oSql;

        if (is_object($_id)) {
            $this->arrData   = (array) $_id;
            $this->elementId = $_id->RouteName;
        } elseif (is_string($_id)) {
            $this->elementId   = $_id;
            if (!$this->load()) {
                $e = new Exception();
                trigger_error('<pre>'.$e->getTraceAsString().'<br /><b>'.$this->dbTableName.':</b> error while loading dataset <b>'.$_id.'</b></pre>', E_USER_WARNING);
            }
        } else {
            trigger_error('Invalid numeric value for Parameter 2 _id', E_USER_WARNING);
        }
    }


    /**
     * Liest die Datenbanktabelle aus uns speichert die Werte lokal ab
     * @return bool
     * */
    private function load ()
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.'
        					  WHERE RouteName=:id',
                            array('id' => $this->elementId));
        if ($oDbResult->getCount() == 0) {
            return false;
        }

        $this->arrData = $oDbResult->getList();
        return true;
    }


    /**
     * Speichert die Daten im Array in der Datenbank ab
     * @return bool
     * */
    public function save()
    {
        $result = false;
        $dbTableName = $this->dbTableName;
        $id          = $this->elementId;
        $query       = '';

        if (count($this->arrData) >= 1) {
            while(list($name, $value) = each($this->arrData)) {
                if (!is_numeric($name) && $name != 'Id' && $name != 'Timestamp') {
                    $query .= $name."='".$value."',";
                }
            }
            reset($this->arrData);

            $query = substr($query, 0, strlen($query)-1);
            $result = $this->oSql->request("UPDATE $dbTableName SET $query WHERE Id='$id'",
                                           array());
        } else {
            $result = false;
        }

        return $result;
    }


    /**
     * Hilfsmethode zur Ausgabe eines Wertes
     * @param string $valueName	: Name des Datenbankrelation
     * @return string|NULL
     * */
    private function getValue ($valueName)
    {
        if (!isset($this->arrData[$valueName])) {
            trigger_error('<b>'.$valueName.'</b > data not available', E_USER_WARNING);
            return NULL;
        }

        return $this->arrData[$valueName];
    }


    /**
     * Liefert den Routen-Name
     * @return string
     * */
    public function getRouteName ()
    {
        return $this->getValue('RouteName');
    }

    /**
     * Definiert den Routen-Name
     * @return string
     * */
    public function setRouteName ($_name)
    {
        if (!is_string($_name)) {
            return $this->arrValid['StaticRoute RouteName'] = false;
        }

        $this->arrData['RouteName'] = $_name;
        return $this->arrValid['StaticRoute RouteName'] = true;
    }


    /**
     * Liefert die Url-Parameter
     * @param string $url 	: Aktueller URI-String
     * @return array|NULL
     * */
    public function getParam ($url)
    {
        if (preg_match($this->getValue('RegExpr'), $url, $arrResult)) {
            return (array) $arrResult;
        } else {
            return NULL;
        }
    }


    /**
     * Liefert den Controller-Name
     * @return string
     * */
    public function getController ()
    {
        return $this->getValue('ControllerName');
    }

    /**
     * Definiert den Controller
     * @return string
     * */
    public function setController ($_name)
    {
        if (!is_string($_name)) {
            return $this->arrValid['StaticRoute Controller'] = false;
        }

        $this->arrData['Controller'] = $_name;
        return $this->arrValid['StaticRoute Controller'] = true;
    }


    /**
     * Liefert den Action-Name
     * @return string
     * */
    public function getAction ()
    {
        return $this->getValue('ActionName');
    }

    /**
     * Definiert die Action
     * @return string
     * */
    public function setAction ($_name)
    {
        if (!is_string($_name)) {
            return $this->arrValid['StaticRoute Action'] = false;
        }

        $this->arrData['Action'] = $_name;
        return $this->arrValid['StaticRoute Action'] = true;
    }


    /**
     * Liefert den View-Name
     * @return string
     * */
    public function getView ()
    {
        return $this->getValue('ViewName');
    }

    /**
     * Definiert die View
     * @return string
     * */
    public function setView ($_name)
    {
        if (!is_string($_name)) {
            return $this->arrValid['StaticRoute View'] = false;
        }

        $this->arrData['View'] = $_name;
        return $this->arrValid['StaticRoute View'] = true;
    }


    /**
     * Liefert durch ersetzen der Variablen den entgueltigen URI-String
     * @param array $arrYapary_Placeholder		: Placeholder mit den Uebersetzungen
     * @return string
     * */
    public function getUrl (array $arrYapary_Placeholder = array())
    {
        $url = vsprintf($this->getValue('Url'), $arrYapary_Placeholder);
        if (!preg_match($this->getValue('RegExpr'), $url)) {
            trigger_error('Url or parameter does not match the regular expression', E_USER_WARNING);
            return NULL;
        }

        return $url;
    }


    /**
     * Liefert den Regulaeren Ausdruck
     * @return string
     * */
    public function getRegExpr ()
    {
        return $this->getValue('RegExpr');
    }

    /**
     * Definiert die Url
     * @return string
     * */
    public function setRegExpr ($_name)
    {
        if (!is_string($_name)) {
            return $this->arrValid['StaticRoute RegExpr'] = false;
        }

        $this->arrData['RegExpr'] = $_name;
        return $this->arrValid['StaticRoute RegExpr'] = true;
    }

    /**
     * Liefert die zusaetzlich gespeicherten Variablen
     * @return array
     * */
    public function getVars ()
    {
        return (array) unserialize($this->getValue('Vars'));
    }

    /**
     * Definiert die Variablen
     * @return string
     * */
    public function setVars (array $_arrName)
    {
        if (!is_array($_arrName)) {
            return $this->arrValid['StaticRoute Vars'] = false;
        }

        $this->arrData['Vars'] = serialize($_arrName);
        return $this->arrValid['StaticRoute Vars'] = true;
    }

}


?>